package com.baichang.taoshunda.utils;

import cn.jpush.api.push.PushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.baichang.framework.utils.ConfigurationHandle;

import java.util.Arrays;
import java.util.List;

public class UserJPushHelper {

    private String secret;
    private String key;
    private boolean isDev;

    private String configName = "jpush.properties";

    public UserJPushHelper() {
        init();
    }

    public UserJPushHelper(String configName) {
        this.configName = configName;
        init();
    }

    private void init() {
        ConfigurationHandle configurationHandle = new ConfigurationHandle(configName);
        secret = configurationHandle.getStringValue("secret");
        key = configurationHandle.getStringValue("key");
        isDev = configurationHandle.getStringValue("isApns").equals("1");
    }

    public boolean sendAll(String title, String content, String sound) {
        return sendAll(title, content, null, sound);
    }

    public boolean sendAll(String title, String content, String data, String sound) {
        Notification notification = createIosAndAndroidNotification(title, content, data, sound);

        PushClient _client = new PushClient(secret, key);
        PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(Audience.all()).setOptions(
                Options.newBuilder().setApnsProduction(isDev).build())
                .setNotification(notification).build();
        return push(_client, payload);
    }

    public boolean sendMsgAll(Message message) {
        PushClient _client = new PushClient(secret, key);
        PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(Audience.all()).setOptions(
                Options.newBuilder().setApnsProduction(isDev).build())
                .setMessage(message)
                .build();
        return push(_client, payload);
    }

    public boolean sendByTagsNoData(String title, String content, String tags, String sound) {
        return sendByTags(title, content, null, tags, sound);
    }

    public boolean sendByTags(String title, String content, String data, String tags, String sound) {
        return sendByTags(title, content, data, Arrays.asList(tags), sound);
    }

    public boolean sendByTags(String title, String content, String data, List<String> tags, String sound) {
        Notification notification = createIosAndAndroidNotification(title, content, data, sound);
        PushClient _client = new PushClient(secret, key);
        PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(Audience.tag(tags)).setOptions(
                Options.newBuilder().setApnsProduction(isDev).build())
                .setNotification(notification).build();
        return push(_client, payload);
    }

    public boolean sendMsgByTags(Message message, String... tags) {
        PushClient _client = new PushClient(secret, key);
        PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(Audience.tag(Arrays.asList(tags))).setOptions(
                Options.newBuilder().setApnsProduction(isDev).build())
                .setMessage(message)
                .build();
        return push(_client, payload);
    }

    private boolean push(PushClient client, PushPayload payload) {
        try {
            PushResult result = client.sendPush(payload);
            if (result.isResultOK()) {
                if (result.sendno != 0) {
                    return true;
                }
            }
        } catch (Exception e) {
            return false;
        }
        return false;
    }


    public boolean sendByAliasNoData(String title, String content, String alias, String sound) {
        return sendByAlias(title, content, null, alias, sound);
    }

    public boolean sendByAlias(String title, String content, String data, String alias, String sound) {
        return sendByAlias(title, content, data, Arrays.asList(alias), sound);
    }

    public boolean sendByAlias(String title, String content, String data, List<String> alias, String sound) {

        Notification notification = createIosAndAndroidNotification(title, content, data, sound);
        PushClient _client = new PushClient(secret, key);
        PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(Audience.alias(alias)).setOptions(
                Options.newBuilder().setApnsProduction(isDev).build())
                .setNotification(notification).build();
        return push(_client, payload);
    }

    public boolean sendMsgByAlias(Message message, String... alias) {
        PushClient _client = new PushClient(secret, key);
        PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(Audience.alias(Arrays.asList(alias))).setOptions(
                Options.newBuilder().setApnsProduction(isDev).build())
                .setMessage(message)
                .build();
        return push(_client, payload);
    }

    private Notification createIosAndAndroidNotification(String title, String content, String data, String sound) {
        AndroidNotification.Builder androidbuilder = AndroidNotification.newBuilder().setTitle(title);
        if (data != null) {
            androidbuilder.addExtra("data", data);
        }
        AndroidNotification android = androidbuilder.setAlert(content).build();

        IosNotification.Builder iosbuilder = IosNotification.newBuilder().setSound(sound);
        iosbuilder.addExtra("content", title);
        if (data != null) {
            iosbuilder.addExtra("data", data);
        }
        IosNotification ios = iosbuilder.setAlert(content).autoBadge().build();

        Notification notification = Notification.newBuilder()
                .addPlatformNotification(android)
                .addPlatformNotification(ios).build();

        return notification;
    }

}
